import Vue from "vue";
import axios from "axios";
import { Toast } from "vant";
import { getStoarge } from "@/utils/storage.js";

Vue.use(Toast);

let http = axios.create({
  baseURL: "http://vt.ossjk.com/",
  timeout: 10000,
  headers: { "content-type": "application/json" },
});

// 添加请求拦截器
http.interceptors.request.use(
  function (config) {
    Toast.loading({
      message: "加载中...",
      forbidClick: true,
    });
    let token = getStoarge("jwtToken");
    if (token) {
      config.headers.token = token;
    }
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 添加响应拦截器
http.interceptors.response.use(
  function (response) {
    // 对响应数据做点什么
    return response;
  },
  function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

Vue.prototype.$http = http;
